package ma.xin.shi.configurations.Service;

import java.util.ArrayList;
import java.util.List;

public class ServiceEntry {
    private String serviceName;
    private int serviceId;
    private String groupId;
    //private String groupName;
    private String prFlg;     // service is [ PROVIDED or REQUIRED ]
    
    private List<ServiceEventConfig> events = new ArrayList<>();
    private List<ServiceFieldConfig> fields = new ArrayList<>();
    private List<ServiceMethodConfig> methods = new ArrayList<>();

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    
    
    //public String getGroupName() {
        //return groupName;
    //}

    //public void setGroupName(String name) {
        //this.groupName = name;
    //}
    

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String id) {
        this.groupId = id;
    }
    
    public String getPrFlg() {
        return prFlg;
    }

    public void setPrFlg(String flg) {
        this.prFlg = flg;
    }
    
    public int getServiceId() {
        return serviceId;
    }

    public void setServiceId(int serviceId) {
        this.serviceId = serviceId;
    }

    public List<ServiceEventConfig> getEvents() {
        return events;
    }

    public List<ServiceFieldConfig> getFields() {
        return fields;
    }

    public List<ServiceMethodConfig> getMethods() {
        return methods;
    }

    // 添加事件
    public boolean addEvent(ServiceEventConfig event) {
        if (event == null) return false;
        return events.add(event);
    }

    // 删除事件
    public boolean removeEvent(ServiceEventConfig event) {
        if (event == null) return false;
        return events.remove(event);
    }

    // 修改事件：替换 oldEvent 为 newEvent
    public boolean updateEvent(ServiceEventConfig oldEvent, ServiceEventConfig newEvent) {
        if (oldEvent == null || newEvent == null) return false;

        int index = events.indexOf(oldEvent);
        if (index >= 0) {
            events.set(index, newEvent);
            return true;
        }
        return false;
    }

    public boolean isEventExistFromName(String name) {
    	for (ServiceEventConfig event : events) {
            if (name != null && name.equals(event.getEventName())) {
                return true;
            }
        }
        return false;
    }

    // 查找事件 by 名称
    public ServiceEventConfig findEventByName(String name) {
        for (ServiceEventConfig event : events) {
            if (name != null && name.equals(event.getEventName())) {
                return event;
            }
        }
        return null;
    }

    // 查找事件 by ID
    public ServiceEventConfig findEventById(int id) {
        for (ServiceEventConfig event : events) {
            if (event.getEventId() == id) {
                return event;
            }
        }
        return null;
    }

    //--------------------------------------------------------------
    
    // 添加事件
    public boolean addMethod(ServiceMethodConfig method) {
        if (method == null) return false;
        return methods.add(method);
    }

    // 删除事件
    public boolean removeMethod(ServiceMethodConfig method) {
        if (method == null) return false;
        return methods.remove(method);
    }

    // 修改事件：替换 oldEvent 为 newEvent
    public boolean updateMethod(ServiceMethodConfig oldEvent, ServiceMethodConfig newMethod) {
        if (oldEvent == null || newMethod == null) return false;

        int index = methods.indexOf(oldEvent);
        if (index >= 0) {
        	methods.set(index, newMethod);
            return true;
        }
        return false;
    }

    public boolean isMethodExistFromName(String name) {
    	for (ServiceMethodConfig method : methods) {
            if (name != null && name.equals(method.getMethodName())) {
                return true;
            }
        }
        return false;
    }
    // 查找事件 by 名称
    public ServiceMethodConfig findMethodByName(String name) {
        for (ServiceMethodConfig method : methods) {
            if (name != null && name.equals(method.getMethodName())) {
                return method;
            }
        }
        return null;
    }

    // 查找事件 by ID
    public ServiceMethodConfig findMethodById(int id) {
        for (ServiceMethodConfig method : methods) {
            if (method.getMethodId() == id) {
                return method;
            }
        }
        return null;
    }

    //-----------------------------------------------------------------------
    // 添加事件
    public boolean addField(ServiceFieldConfig method) {
        if (method == null) return false;
        return fields.add(method);
    }

    // 删除事件
    public boolean removeField(ServiceFieldConfig method) {
        if (method == null) return false;
        return fields.remove(method);
    }

    // 修改事件：替换 oldEvent 为 newEvent
    public boolean updateField(ServiceFieldConfig oldField, ServiceFieldConfig newField) {
        if (oldField == null || newField == null) return false;

        int index = fields.indexOf(oldField);
        if (index >= 0) {
        	fields.set(index, newField);
            return true;
        }
        return false;
    }

    // 检查是否存在field
    public boolean isFieldExistFromName(String name) {
    	for (ServiceFieldConfig field : fields) {
            if (name != null && name.equals(field.getFieldName())) {
                return true;
            }
        }
        return false;
    }
    
    // 是否属于某个Field的notifier name
    public boolean isNameBelongFieldNotifier(String name) {
        for (ServiceFieldConfig field : fields) {
            if (name != null && name.equals(field.getNotifierName())) {
                return true;
            }
        }
        return false;
    }
    
    // 查找事件 by 名称
    public ServiceFieldConfig findFieldByName(String name) {
        for (ServiceFieldConfig field : fields) {
            if (name != null && name.equals(field.getFieldName())) {
                return field;
            }
        }
        return null;
    }

    // 查找事件 by ID
    public ServiceFieldConfig findFieldById(int id) {
        for (ServiceFieldConfig field : fields) {
            if (field.getFieldId() == id) {
                return field;
            }
        }
        return null;
    }
    
    // 查找事件 by 名称
    public ServiceFieldConfig findFieldByNotifierName(String name) {
        for (ServiceFieldConfig field : fields) {
            if (name != null && name.equals(field.getNotifierName())) {
                return field;
            }
        }
        return null;
    }
    
    // 确认是否有filed Notifier
    public boolean isServiceFieldNotifierExist() {
    	if (!(null == fields || fields.isEmpty())) {
    		for (ServiceFieldConfig field : fields) {
    			if (field.getNotifierStatus() == true) {
    				return true;
    			}
    		}
    	}

    	return false;
    }
}
